#ifndef _DXYEVENT_H_
#define _DXYEVENT_H_

#include <functional>
#include <sys/epoll.h>
#include <exception>
#include <map>

namespace dxy::event
{

class DxyEvent
{
public:
    DxyEvent();
    ~DxyEvent();

    template<class T, class... Args>
    void addEvent(int fd, uint32_t flag, std::function<T> cb, Args&&... args);

    void delEvent(int fd);

    void eventDispatch();

private:
    int                                         m_epollfd           = -1;
    struct epoll_event                          m_events[1024];
    std::map<int, std::function<void (void)>>   m_eventsCbs;

};

template<class T, class... Args>
void DxyEvent::addEvent(int fd, uint32_t flag, std::function<T> cb, Args&&... args)
{
    struct epoll_event ev;
    ev.data.fd = fd;
    ev.events = flag;
    auto func = std::bind(cb, std::forward<Args>(args)...);
    m_eventsCbs[fd] = [func] () {func();};
    epoll_ctl(m_epollfd, EPOLL_CTL_ADD, fd, &ev);
}

}


#endif